<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\controller\Base;
use app\common\model\Options;

class Setting extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new Options();
    }

    // 全站设置
  public function site()
  {
    $option_name = "site_options";
    if($this->request->isPost()){
      $params = $this->request->post();
      $data = $params["options"];
      foreach ($data as $key => $value) {
        $data[$key] = trim($value);
      }

      $res = $this->Model->saveSetting($option_name, $data);
      if($res['status']){
        Session::flash("success", $res['msg']);
      }else{
        Session::flash("error", $res['msg']);
      }

      $this->redirect($this->request->action());
    }

    $options = $this->Model->readSetting($option_name);
    $this->assign("options", $options);
    return $this->fetch();
  }

  // smtp设置
  public function smtp()
  {
    $option_name = "smtp_options";
    if($this->request->isPost()){
      $params = $this->request->post();
      $data = $params["options"];
      foreach ($data as $key => $value) {
        $data[$key] = trim($value);
      }
      if(!isset($data["smtp_open"])){
        // 不启用smtp
        $data["smtp_open"] = "0";
      }
      $res = $this->Model->saveSetting($option_name, $data);
      if($res['status']){
        Session::flash("success", $res['msg']);
      }else{
        Session::flash("error", $res['msg']);
      }
      $this->redirect($this->request->action());
    }

    $secure_list = ["ssl" => "普通连接方式", "tls" => "TLS连接方式"];
    $this->assign("secure_list", $secure_list);

    $options = $this->Model->readSetting($option_name);
    if(isset($options["smtp_secure"])){
      $smtp_secure = $options["smtp_secure"];
    }else{
      $smtp_secure = "";
    }

    $this->assign("options", $options);
    $this->assign("smtp_secure", $smtp_secure);
    return $this->fetch();
  }

  // url model设置
  public function url()
  {
    $option_name = "url_options";
    if($this->request->isPost()){
      $params = $this->request->post();
      $data = $params["options"];
      foreach ($data as $key => $value) {
        $data[$key] = trim($value);
      }
      $res = $this->Model->saveSetting($option_name, $data);
      if($res['status']){
        Session::flash("success", $res['msg']);
      }else{
        Session::flash("error", $res['msg']);
      }
      $this->redirect($this->request->action());
    }

    $model_list = ["0" => "普通模式", "1" => "PATHINFO模式", "2" => "REWRITE模式", "3" => "兼容模式"];
    $this->assign("model_list", $model_list);

    $options = $this->Model->readSetting($option_name);
    if(isset($options["url_model"])){
      $url_model = $options["url_model"];
    }else{
      $url_model = "2";
    }

    $this->assign("options", $options);
    $this->assign("url_model", $url_model);
    return $this->fetch();
  }

  // 激活邮件
  public function active()
  {
    $option_name = "active_options";
    if($this->request->isPost()){
      $params = $this->request->post();
      $data = $params["options"];
      foreach ($data as $key => $value) {
        $data[$key] = trim($value);
      }
      if(!isset($data["email_active"])){
        // 不启用email active
        $data["email_active"] = "0";
      }
      $res = $this->Model->saveSetting($option_name, $data);
      if($res['status']){
        Session::flash("success", $res['msg']);
      }else{
        Session::flash("error", $res['msg']);
      }
      $this->redirect($this->request->action());
    }

    $options = $this->Model->readSetting($option_name);
    $this->assign("options", $options);
    return $this->fetch();
  }

  // 第三方授权登录
  public function oauth()
  {
    $option_name = "oauth_options";
    if($this->request->isPost()){
      $params = $this->request->post();
      $data = $params["options"];
      foreach ($data as $key => $value) {
        $data[$key] = trim($value);
      }
      $res = $this->Model->saveSetting($option_name, $data);
      if($res['status']){
        Session::flash("success", $res['msg']);
      }else{
        Session::flash("error", $res['msg']);
      }
      $this->redirect($this->request->action());
    }

    $options = $this->Model->readSetting($option_name);
    $this->assign("options", $options);
    return $this->fetch();
  }
}
